package diy.container
{
	import diy.Config;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	import mx.core.UIComponent;
	
	public class EditorContainer extends UIComponent
	{
		[Embed(source="phone1.png")]
		private var phoneClass:Class;
		[Embed(source="editor_bg.jpg")]
		private var bgClass:Class;
		[Embed(source="phonelook.png")]
		private var lookClass:Class;
		
		public static var instance:EditorContainer;
		public static var selectType:int;
		public function EditorContainer()
		{
			super();
			instance = this;
			width = 870;
			height = 450;
			init();
			addEventListener(MouseEvent.MOUSE_MOVE,onMove);
		}
		public var scBmp:Bitmap;
		private var maskLayer:Sprite;
		private var zoomLayer:Sprite;
		private var modelData:Object;
		public var picLayer:UIComponent;
		private var decorLayer:Sprite;
		private var model:Sprite;
		private function init():void{
			zoomLayer = new Sprite();
			zoomLayer.scaleX = 0.5;
			zoomLayer.scaleY = 0.5;
			zoomLayer.blendMode = BlendMode.LAYER;
			var scPhone:DisplayObject = new phoneClass();
			scPhone.blendMode = BlendMode.ALPHA;
			scBmp = new Bitmap();
			zoomLayer.addChild(new phoneClass());
			zoomLayer.addChild(scBmp);
			zoomLayer.addChild(scPhone);
			
			picLayer = new UIComponent();
			decorLayer = new Sprite();
			
			model = new Sprite();
			model.addChild(new phoneClass());
			model.x = (width-model.width)/2;
			model.y = (height-model.height)/2;
			model.mouseEnabled = model.mouseChildren = false;
			
			maskLayer = new Sprite();
			maskLayer.graphics.beginFill(0xffffff,0.5);
			maskLayer.graphics.drawRect(0,0,width,height);
			maskLayer.blendMode = BlendMode.LAYER;
			
			var sp:Sprite = new Sprite();
			sp.addChild(new phoneClass());
			sp.x = (width-sp.width)/2;
			sp.y = (height-sp.height)/2;
			sp.blendMode = BlendMode.ERASE;
			
			maskLayer.mouseEnabled = maskLayer.mouseChildren = false;
			maskLayer.addChild(sp);
			//maskLayer.mask = sp;
			//addChild(sp);
			
			var bg:DisplayObject = new bgClass();
			bg.width = width;
			bg.height = height;
			
			graphics.beginFill(0xcccccc);
			graphics.drawRect(0,0,width,height);
			
			//addChild(bg);
			addChild(model);
			addChild(picLayer);
			addChild(decorLayer);
			addChild(maskLayer);
			addChild(zoomLayer);
			
			var dis:DisplayObject = new lookClass();
			dis.x = model.x + 13;
			dis.y = model.y + 10;
			decorLayer.addChild(dis);
			
			var m:Sprite = new Sprite();
			m.graphics.beginFill(0x00ff00);
			m.graphics.drawRect(0,0,width,height);
			addChild(m);
			this.mask = m;
		}
		private function onMove(evt:MouseEvent):void{
			if(!evt.buttonDown)
				return;
			var bmp:BitmapData = new BitmapData(maskLayer.width,maskLayer.height,true,0x00ffffff);
			var m:Matrix = new Matrix();
			//m.a = 0.5;
			//m.d = 0.5;
			bmp.draw(maskLayer,m);
			
			var bp:Bitmap = new Bitmap(bmp);
			bp.blendMode = BlendMode.ERASE;
			
			var bmp3:BitmapData = new BitmapData(maskLayer.width,maskLayer.height,true,0x00ffffff); 
			bmp3.draw(picLayer,new Matrix());
			var bp3:Bitmap = new Bitmap(bmp3);
			
			var sp1:Sprite = new Sprite();
			sp1.blendMode = BlendMode.LAYER;
			sp1.addChild(bp3);
			sp1.addChild(bp);
			
			//while(zoomLayer.numChildren)
			//	zoomLayer.removeChildAt(0);
			//zoomLayer.addChild(sp1);
			var bmp5:BitmapData = new BitmapData(maskLayer.width,maskLayer.height,true,0x00ffffff); 
			bmp5.draw(sp1,new Matrix());
			
			var clipRect:Rectangle = new Rectangle(model.x,model.y,model.width,model.height);
			var result:BitmapData = new BitmapData(clipRect.width,clipRect.height,bmp5.transparent,0x00ffffff);
			result.copyPixels(bmp5,clipRect,new Point());
			/*
			var bmp2:BitmapData = new BitmapData(167,223);
			var m:Matrix = new Matrix();
			m.a = 0.5;
			m.d = 0.5;
			bmp2.draw(sp1,m,null,null);
			*/
			bmp.dispose();
			bmp3.dispose();
			bmp5.dispose();
			if(scBmp.bitmapData)
				scBmp.bitmapData.dispose();
			scBmp.bitmapData = result;
		}
		
		public function getZoomBmp():BitmapData{
			var bmpData:BitmapData = new BitmapData(scBmp.width*0.5,scBmp.height*0.5,true,0x00ffffff);
			var matrix:Matrix = new Matrix();
			matrix.scale(0.5,0.5);
			bmpData.draw(zoomLayer,matrix);
			return bmpData;
		}
		
		public function changeModel(data:Object):void{
			modelData = data;
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
			loader.load(new URLRequest(Config.path+"/images/product/"+data.imagesbg));
		}
		
		protected function onComplete(event:Event):void
		{
			var loader:LoaderInfo = event.target as LoaderInfo;
			//创建一个大小和截图对象一致的图片 
			var bmpData:BitmapData = new BitmapData(loader.width*0.35,loader.height*0.35,true,0x00ffffff); 
			//进行快照截图，其中第二个参数Matrix是对图片进行变换用的，例如旋转缩放等。 
			//如果图片不需要变换可以用new Matrix()或者null都可以。 
			var matrix:Matrix = new Matrix();
			matrix.scale(0.35,0.35);
			bmpData.draw(loader.content,matrix); 
			
			var bitMap:Bitmap = new Bitmap(bmpData);
			setModel(bitMap);
			setZoomBg(bitMap);
			setMask(bitMap);
		}
		private function setModel(bitMap:Bitmap):void{
			while(model.numChildren)
				model.removeChildAt(0);
			model.addChild(bitMap);
			model.x = (width-bitMap.width)/2;
			model.y = (height-bitMap.height)/2;
		}
		private function setZoomBg(bitMap:Bitmap):void{
			while(zoomLayer.numChildren)
				zoomLayer.removeChildAt(0);
			var data:BitmapData = bitMap.bitmapData.clone();
			var scPhone:Bitmap = new Bitmap(data);
			scPhone.blendMode = BlendMode.ALPHA;
			scBmp = new Bitmap();
			zoomLayer.addChild(new Bitmap(data));
			zoomLayer.addChild(scBmp);
			
			var lookLoader:Loader = new Loader();
			lookLoader.load(new URLRequest(Config.path+"/images/product/"+modelData.lookimages));
			lookLoader.x = modelData.px;
			lookLoader.y = modelData.py;
			
			zoomLayer.addChild(lookLoader);
			zoomLayer.addChild(scPhone);
		}
		private function setMask(bitMap:Bitmap):void{
			var data:BitmapData = bitMap.bitmapData.clone();
			var scPhone:Bitmap = new Bitmap(data);
			var sp:Sprite = new Sprite();
			sp.addChild(scPhone);
			sp.x = (width-sp.width)/2;
			sp.y = (height-sp.height)/2;
			sp.blendMode = BlendMode.ERASE;
			while(maskLayer.numChildren)
				maskLayer.removeChildAt(0);
			maskLayer.addChild(sp);
		}
	}
}